<template>
  <div>
    <home-header/>
    <div class="content" ref="content">
      <div>
        <home-swiper :swiperList="swiperList"/>
        <home-icons :list="iconList"/>
        <home-recommend :list="recommendList"/>
        <home-weekend :list="weekendList"/>
      </div>
    </div>
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import BScroll from 'better-scroll'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data () {
    return {
      lastCity: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    ...mapState(['city'])
  },
  created () {
    this.getHomeInfo()
  },
  mounted () {
    this.lastCity = this.city
    this.scroll = new BScroll(this.$refs.content)
  },
  activated () {
    // 当城市发生改变时请求数据
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/home.json?city=' + this.city).then(({ data }) => {
        if (data && data.code === 0) {
          this.swiperList = data.data.swiperList
          this.iconList = data.data.iconList
          this.recommendList = data.data.recommendList
          this.weekendList = data.data.weekendList
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content{
  position: absolute;
  z-index: -1;
  top: .86rem;
  right: 0;
  bottom: 0;
  left: 0;
}
</style>
